package com.pl.service.impl;  

import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pl.bean.ManageUserRoleBean;
import com.pl.dao.ManageUserRoleBeanDao;
import com.pl.service.ManageUserRoleService;
import com.pl.util.CommonUtil;

import javax.annotation.Resource;

/**
 * 
 */
@Service("manageUserRoleService")
public class ManageUserRoleServiceImpl implements ManageUserRoleService{

	private static Logger log = LogManager.getLogger();

	@Resource(name="manageUserRoleBeanDao")
	private ManageUserRoleBeanDao manageUserRoleBeanDao;

	/**
	 * 新增
	 */
	@Transactional
	public void addManageUserRole(ManageUserRoleBean manageUserRoleBean){
		
		manageUserRoleBeanDao.addManageUserRoleBean(manageUserRoleBean);
	}
	
	/**
	 * 新增
	 */
	@Transactional
	public void addManageUserRole(long roleId, List<Long> userIdList, long operateId){
		
		if(userIdList !=null && !userIdList.isEmpty()){
			for (Long userId : userIdList) {
				
				ManageUserRoleBean bean = new ManageUserRoleBean();
				bean.setManage_user_id(userId);
				bean.setManage_role_id(roleId);
				bean.setOperate_id(operateId);
				bean.setInsert_time(CommonUtil.getCurrentDatetime());
				bean.setUpdate_time(CommonUtil.getCurrentDatetime());
				
				manageUserRoleBeanDao.addManageUserRoleBean(bean);
			}
		}
	}

	/**
	 * 修改
	 */
	@Transactional
	public void updateManageUserRole(ManageUserRoleBean manageUserRoleBean){
		
		manageUserRoleBeanDao.updateManageUserRoleBean(manageUserRoleBean);
	}
	
	/**
	 * 删除用户角色
	 */
	@Transactional
	public void deleteUserRole(long userId, long roleId){
		
		manageUserRoleBeanDao.deleteUserRole(userId, roleId);
	}
	
	/**
	 * 根据条件获得数量
	 */
	public int getManageUserRoleNum(ManageUserRoleBean manageUserRoleBean){
		
		return manageUserRoleBeanDao.getManageUserRoleBeanNum(manageUserRoleBean);
	}
	
	/**
	 * 根据条件获得数据
	 */
	public List<ManageUserRoleBean> getManageUserRole(ManageUserRoleBean manageUserRoleBean){
		
		return manageUserRoleBeanDao.getManageUserRoleBean(manageUserRoleBean);
	}
	
	/**
	 * 根据条件获得数据
	 */
	public ManageUserRoleBean getManageUserRoleById(Long id){
		
		ManageUserRoleBean manageUserRoleBean = new ManageUserRoleBean();
		manageUserRoleBean.setId(id);
		
		List<ManageUserRoleBean> list = getManageUserRole(manageUserRoleBean);
		if(list !=null && !list.isEmpty()){
    		return list.get(0);
    	}
    	
    	return null;
	}
}
